package com.bahiaapk;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bahiaapk.dao.NewsDAO;
import com.bahiaapk.dto.News;
import com.bahiaapk.services.SynchronizeUpdateNewsRanking;
import com.raptureinvenice.webimageview.image.WebImageView;

@SuppressLint("HandlerLeak")
public class NewsDetailActivity extends Activity{

	
	private WebView webView;
	private TextView titleTX;
	private TextView dateTX;
	private SynchronizeUpdateNewsRanking serviceUpdateNewsRanking;
	private LinearLayout layoutWebview;
	private Display display;
	private Integer heightScreen;
	private WebImageView webImageView;
	private News newsDetail;
	private NewsDAO dao;
	
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_detail);
        
        layoutWebview = (LinearLayout) findViewById(R.id.layout_webview);
        titleTX= (TextView) findViewById(R.id.titleTX);
        webView= (WebView) findViewById(R.id.webView);
        dateTX= (TextView) findViewById(R.id.dateTX);
        webImageView= (WebImageView) findViewById(R.id.image);
        
        fixScreenSize();
        
        Bundle loadInfo = getIntent().getExtras();
        newsDetail = (News) loadInfo.getSerializable("news");
        
		dao = new NewsDAO(this);

		newsDetail = dao.findByPrimaryKey(newsDetail);
		
        if(newsDetail!=null && newsDetail.getSent()==0){
        	Handler handler = new Handler(){
        		@Override
     	 		public void handleMessage(Message msg){
        			dao.updateSent(newsDetail);
        		}

			
    		};
    		serviceUpdateNewsRanking = new SynchronizeUpdateNewsRanking();
    		serviceUpdateNewsRanking.synchronize(this, handler, newsDetail);
        }
        
        detail();       
      
    }

	

	private void fixScreenSize() {
		display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        
        heightScreen = display.getHeight();
        int newValue = (int) (heightScreen*0.68);
        
       
        
        layoutWebview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, newValue));
		
	}
	
	

	private void detail() {
		
		titleTX.setText(newsDetail.getTitle());
		
		dateTX.setText(newsDetail.getDateFormat());
		
		webImageView.setImageWithURL(this,newsDetail.getUrlImage());
		
		webView.loadDataWithBaseURL(null, newsDetail.getContent(), "text/html", "utf-8", null);
		
	}
	
	public void sendEmail(View view){
		
		Intent gmail = new Intent(Intent.ACTION_VIEW);
		gmail.setComponent(new ComponentName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail"));
	    gmail.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
	    gmail.putExtra(Intent.EXTRA_SUBJECT, "EKUPDATE12: "+"test");
	    gmail.setType("plain/text");
	    gmail.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("Via de app EKUPDATE12 kwam ik dit nieuwsbericht tegen: '"+"teste"+"' <br/><br/> <a href=http://www.beleefsport2012.nl/content/download-app>klik hier</a> om de gratis EKUPDATE12 app te downloaden."));
	        
	    try {
	    	startActivity(gmail);
	    } catch (android.content.ActivityNotFoundException ex) {
			return;
	    }
	    return;
	}
	public void sendTwitter(View view){
//		Intent intent = new Intent(this, NewsTwitterActivity.class);//ServiceSelectActivity.class);
//		intent.putExtra("news", newsDetail);
//		startActivity(intent);

	}
	public void sendFacebook(View view){
//		Intent intent = new Intent(this, NewsFacebookActivity.class);//ServiceSelectActivity.class);
//		intent.putExtra("news", newsDetail);
//		startActivity(intent);

	}
	// [NSString stringWithFormat:@"Via de app EKUPDATE12 kwam ik dit nieuwsbericht tegen: \"%@\"<br/><br/> <a href=\"%@\">klik hier</a> om de gratis EKUPDATE12 app te downloaden.", theTitle, SHARE_LINK]
	// 
	

}
